<%@page import="java.util.ArrayList"%>
<%@page import="org.firestorm.traficforecast.astar.FindWay"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.Map"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.firestorm.traficforecast.database.Road"%>
<%@page import="org.firestorm.traficforecast.database.RoadDAO"%>
<%@page import="org.firestorm.traficforecast.database.User"%>
<%@page import="org.firestorm.traficforecast.utils.Md5"%>
<%@page import="org.firestorm.traficforecast.utils.Constant"%>
<%@page import="org.firestorm.traficforecast.database.UserDAO"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONValue"%>

<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf8");
	response.setContentType("application/json");
	PrintWriter outt = response.getWriter();
	JSONObject objDirect = (JSONObject) JSONValue.parse(request
			.getParameter("para"));
	String direct1 = objDirect.get("direct1").toString();
	String direct2 = objDirect.get("direct2").toString();
	String[] starts = direct1.split(", ");
	// 	System.out.println(starts[0]);
	String[] finishs = direct2.split(", ");
	// 	System.out.print(finishs[0]);

	Road start = null;
	Road finish = null;

	boolean check = false;
	if (starts[0] == null || "".equals(starts[0])) {
		check = false;
	} else {
		if (finishs[0] == null || "".equals(finishs[0])) {
			check = false;
		} else {
			start = RoadDAO.getRoadbyName(starts[0]);
			finish = RoadDAO.getRoadbyName(finishs[0]);
		}
	}

	FindWay fw = new FindWay();

	// 	Road start = RoadDAO.getRoad("1170807501");
	// 	Road finish = RoadDAO.getRoad("218106748910");
	if (start != null && finish != null) {
		System.out.println(start.getName() + " to " + finish.getName());
		ArrayList<Road> vd = fw.aStar(start, finish);

		if (vd.isEmpty()) {
			HashMap<String, Boolean> status = new HashMap<String, Boolean>();
			status.put("status", false);
			JSONObject statusobj = new JSONObject(status);

			JSONArray jsonarr = new JSONArray();
			jsonarr.add(status);

			JSONObject result = new JSONObject();
			result.put("arr", jsonarr);

			out.print(result);
			out.flush();
		} else {
			HashMap<String, Boolean> status = new HashMap<String, Boolean>();
			status.put("status", true);
			JSONObject statusobj = new JSONObject(status);

			JSONArray jsonarr = new JSONArray();
			jsonarr.add(status);

			System.out.println(vd.size());
			//         for(int i=0;i<vd.size()-1;i++){
			//             if(!(vd.get(i).getName().equals(vd.get(i+1).getName()))){
			//                 System.out.print(vd.get(i).getName() +"("+ vd.get(i).getStatus() +")");
			//                 System.out.print("==>");
			//             }           

			//         }
			for (int i = 0; i < vd.size() - 1; i++) {
				if (!(vd.get(i).getName().equals(vd.get(i + 1)
						.getName()))) {
					HashMap<String, String> t = new HashMap<String, String>();

					t.put("lat", "" + vd.get(i).getStart().getLat());
					t.put("lng", "" + vd.get(i).getStart().getLon());
					t.put("name", vd.get(i).getName());
					t.put("status", "" + vd.get(i).getStatus());

					JSONObject jsonobj = new JSONObject(t);
					jsonarr.add(jsonobj);
				}
			}

			JSONObject result = new JSONObject();
			result.put("arr", jsonarr);

			out.print(result);
			out.flush();
		}
	} else {
		HashMap<String, Boolean> status = new HashMap<String, Boolean>();
		status.put("status", false);
		JSONObject statusobj = new JSONObject(status);

		JSONArray jsonarr = new JSONArray();
		jsonarr.add(status);

		JSONObject result = new JSONObject();
		result.put("arr", jsonarr);

		out.print(result);
		out.flush();
	}
%>